package com.ai.json;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: MrHe
 * @create: 2023-12-13 23:53
 * @description:
 **/

public class JsonDemo {
    public static void main(String[] args) {
        // deal(4, 2, 4);
        deal(4, 2, 3);
    }

    private static void deal(Integer total,Integer min ,Integer  max) {
        List<JSONObject> list = dealList(total, min, max);
        // 如果当前是转人工 并且下一轮不是转人工 记录 并替换成转人工
        // 如果当前是 不是转人工,但是下一轮是转人工 记录下来
        dealTransfer(list);
        System.out.println("list = " + list);
    }

    private static void dealTransfer(List<JSONObject> list) {
        Map<Integer, List<JSONObject>> map = list.stream().collect(Collectors.groupingBy(t -> t.getInteger("asrId")));
        Integer lastAsrId = 0;
        Integer time = 0;
        Map<String, List<JSONObject>> fromToMap = new HashMap<>();
        int maxSize = map.size();
        for (Integer asrId : map.keySet()) {
            if (asrId > lastAsrId) {
                lastAsrId = asrId;
            }
            List<JSONObject> current = map.get(asrId);
            // 当前不是 但是下一轮是 记录下来
            if (asrId + 1 <= maxSize) {
                List<JSONObject> next = map.get(asrId + 1);
                JSONObject currentJ = current.get(0);
                JSONObject nextJ = next.get(0);
                String nextSence = nextJ.getString("场景");
                String currentSence = currentJ.getString("场景");
                if (!currentSence.equalsIgnoreCase("转人工")
                        && nextSence.equalsIgnoreCase("转人工")
                ) {
                    fromToMap.put("FROM" + time, current);

                }
                if (!nextSence.equalsIgnoreCase("转人工")
                        && currentSence.equalsIgnoreCase("转人工")
                ) {
                    fromToMap.put("TO" + time, current);
                    time++;
                }
            }

        }
        // 逻辑处理,进行替换
        if (fromToMap.size() >= 1) {
            for (String from2Key : fromToMap.keySet()) {
                if (from2Key.contains("FROM")){
                    List<JSONObject> fromJsonList = fromToMap.get(from2Key);
                    // 获取要被替换的
                    String toKey = from2Key.replace("FROM", "TO");
                    List<JSONObject> toJsonList = fromToMap.get(toKey);
                    if (CollectionUtils.isEmpty(toJsonList)){
                        // 以人工介入挂断
                        fromJsonList.forEach(json->{
                            json.put("场景","转人工");
                            json.put("another","-");
                        });
                    }else {
                        JSONObject toJ = toJsonList.get(0);
                        JSONObject fromJ = fromJsonList.get(0);
                        JSONObject realFromJ = new JSONObject();
                        realFromJ.put("场景",fromJ.getString("场景"));
                        realFromJ.put("another",fromJ.getString("another"));

                        fromJsonList.forEach(json->{
                            json.put("场景",toJ.getString("场景"));
                            json.put("another",toJ.getString("another"));
                        });
                        toJsonList.forEach(json->{
                            json.put("场景",realFromJ.getString("场景"));
                            json.put("another",realFromJ.getString("another"));
                        });
                    }
                }
            }
        }
    }

    private static List<JSONObject> dealList(  Integer total, Integer min, Integer max) {
        List<JSONObject> list = new ArrayList<>();

        for (int i = 1; i <= total; i++) {
            JSONObject user = new JSONObject();
            user.put("asrId", i);
            user.put("content", "内容_" + i);

            JSONObject ai = new JSONObject();
            ai.put("asrId", i);
            ai.put("content", "内容_" + i);


            ai.put("another", "another_" + i);
            user.put("another", "another_" + i);
            if (i >= min && i <= max) {
                ai.put("场景", "转人工");
                user.put("场景", "转人工");
            }else {
                ai.put("场景", "another_"+i);
                user.put("场景", "another_"+i);
            }
            list.add(ai);
            list.add(user);
        }
        return list;
    }

}
